// ----------------------------------------------------------------------------
// serialize_pointer_member.cpp
// ----------------------------------------------------------------------------
#include <fw/base/serialize/serialize_pointer_member.h>
#include <fw/base/serialize/serialize_allocator.h>
#include <fw/base/serialize/serialize_type.h>
#include <fw/base/serialize/serialize_schema.h>
#include <fw/base/serialize/serialize_status.h>
#include <fw/base/serialize/byte_reader.h>
#include <fw/base/serialize/byte_writer.h>

namespace fw
{
	SerializePointerMember::SerializePointerMember(int count, Hash typeName)
	: SerializeMember(count, typeName)
	, m_type(nullptr)
	{
	}
	
	SerializePointerMember::~SerializePointerMember()
	{
		if(m_type)
		{
			delete m_type;
		}
	}
	
	size_t SerializePointerMember::GetSize() const
	{
		return GetType()->GetSize();
	}
	
	size_t SerializePointerMember::GetAlignment() const
	{
		return GetType()->GetAlignment();
	}
	
	void SerializePointerMember::Convert(char*					dstData,
										 const char*			srcData,
										 const SerializeMember&	srcMember,
										 SerializeAllocator&	alloc) const
	{
		for(int index=0; index<GetCount(); ++index)
		{
			char*       dstMemberData = GetDataPtr(dstData, index);
			const char* srcMemberData = srcMember.GetDataPtr(srcData, index);
			
			int count = srcMember.GetPointerCount(srcData, index);
			GetType()->ConvertOne(dstMemberData,
								  srcMemberData,
								  srcMember.GetType(),
								  alloc,
								  count);
		}
	}
	
	const SerializeType* SerializePointerMember::PreFixUp(SerializeSchema &schema, Hash typeName)
	{
		const SerializeType* indirectType = schema.FindType(typeName);
		m_type = new SerializePointerType(indirectType);
		return m_type;
	}
	
} // namespace fw
